<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<!-- 下载并引入jquery easyUI -->
<link rel="stylesheet" type="text/css"
	href="../../js/jquery-easyui-1.9.5/themes/default/easyui.css">
<link rel="stylesheet" type="text/css"
	href="../../js/jquery-easyui-1.9.5/themes/icon.css">
<link rel="stylesheet" type="text/css"
	href="../../js/jquery-easyui-1.9.5/themes/color.css">
<script type="text/javascript"
	src="../../js/jquery-easyui-1.9.5/jquery.min.js"></script>
<script type="text/javascript"
	src="../../js/jquery-easyui-1.9.5/jquery.easyui.min.js"></script>
<script type="text/javascript" src="../js/context-menu.js"></script>
</head>
<body>
	<table id="store_dg" title="库存管理" class="easyui-datagrid"
		style="width: 100%; height: 100%" url="../store/list"
		toolbar="#store_toolbar" pagination="true" rownumbers="true"
		fitColumns="true" singleSelect="true" data-options="fit:true">
		<thead>
			<tr>
				<th field="storeNo" width="50">库存编号</th>
				<th field="carBrand" width="50">汽车品牌</th>

				<th field="quantity" width="50">数量</th>
				<th field="inPrice" width="50">进价</th>
				<th field="outPrice" width="50">售价</th>
				<th field="produceDate" width="50">生产日期</th>
				<th field="provider" formatter="providerFormatter" width="50">供应商</th>
				<th field="in" formatter="inFormatter" width="50">进货</th>


			</tr>
		</thead>
	</table>
	<div id="store_toolbar">

		<a href="#" class="easyui-linkbutton" iconCls="icon-add" plain="true"
			onclick="newStore()">添加</a> <a href="#" class="easyui-linkbutton"
			iconCls="icon-edit" plain="true" onclick="editStore()">编辑</a> <a
			href="#" class="easyui-linkbutton" iconCls="icon-remove" plain="true"
			onclick="destroyStore()">删除</a>
	</div>


	<!-- 新增和修改对话框开始 -->
	<!-- 修改每个组件的id号，增加模块标志信息 -->
	<div id="store_dlg" class="easyui-dialog"
		style="width: 400px; height: 280px; padding: 10px 20px" closed="true"
		buttons="#store_dlg-buttons">
		<div class="ftitle">库存信息</div>
		<form id="store_fm" enctype="multipart/form-data" method="post">
			<div class="fitem">
				<label>汽车品牌:</label> <input name="carBrand" class="easyui-textbox">
			</div>
			<div class="fitem">
				<label>数量:</label> <input name="quantity" class="easyui-textbox">
			</div>
			<div class="fitem">
				<label>进价:</label> <input name="inPrice" class="easyui-textbox">
			</div>
			<div class="fitem">
				<label>售价:</label> <input name="outPrice" class="easyui-textbox">
			</div>
			<div class="fitem">
				<label>生产日期:</label> <input name="produceDate" class="easyui-datebox"
					data-options="required:true">
			</div>

			<div class="fitem">
				<label>供应商:</label> <input id="providerNo" name="provider.providerNo"
					class="easyui-combobox"
					data-options="valueField:'providerNo',textField:'provider',url:'../store/providerList'"
					required="true">
			</div>
			<div class="fitem">
				<label>进货:</label> <input id="inNo" name="in.inNo"
					class="easyui-combobox"
					data-options="valueField:'inNo',textField:'inDate',url:'../store/inList'"
					required="true">
			</div>



		</form>
	</div>
	<div id="store_dlg-buttons">
		<a href="#" class="easyui-linkbutton" iconCls="icon-ok"
			onclick="saveStore()">保存</a> <a href="#" class="easyui-linkbutton"
			iconCls="icon-cancel"
			onclick="javascript:$('#store_dlg').dialog('close')">取消</a>
	</div>

	<!-- 新增和修改对话框结束-->
	<script type="text/javascript">
		function providerFormatter(value, row, index) {
			return value.provider;
		}
		function inFormatter(value, row, index) {
			return value.inDate;
		}

		//点击数据表上的新增按钮，会调用这个方法
		function newStore() {
			//打开新增对话框
			$('#store_dlg').dialog('open').dialog('setTitle', '新增库存信息');
			//新增对话框包含了一个form表单，可能存在多次打开，这里对表单进行清空操作
			$('#store_fm').form('clear');
			//设置新增数据时提交到服务器的url地址
			url = '../store/add';
			//新增数据，默认选择商品类型和供应商的第一条数据
			loadProviderSuccess();
			loadInSuccess();
		}
		function loadProviderSuccess(value) {
			$("#providerNo").combobox(
					{
						onLoadSuccess : function(data) {
							if (data) {
								if (value) {
									$("providerNo").combobox('select', value);
								} else {
									$("providerNo").combobox('select',
											data[0].providerNo);
								}
							}
						}
					});
		}
		function loadInSuccess(value) {
			$("inNo").combobox({
				onLoadSuccess : function(data) {
					if (data) {
						if (value) {
							$("inNo").combobox('select', value);
						} else {
							$("inNo").combobox('select', data[0].inNo);
						}
					}
				}
			});
		}
		function editStore() {
			var row = $('#store_dg').datagrid('getSelected');
			if (row) {
				$('#store_dlg').dialog('open').dialog('setTitle', '编辑库存信息');
				$('#store_fm').form('load', row);
				url = '../store/edit?storeNo=' + row.storeNo;
				loadStoreTypeSuccess(row.provider.providerNo);
				loadProviderSuccess(row.in.inNo);
			} else {
				$.messager.show({
					title : 'Error',
					msg : "请选择要修改的库存信息"
				});
			}
		}

		function saveStore() {
			$('#store_fm').form('submit', {
				url : url,
				onSubmit : function() {
					return $(this).form('validate');//提交表单前先进行表单数据验证，验证通过才提交数据到服务器；验证失败不提交数据
				},
				success : function(result) {//收到服务器响应的json字符串后，调用这里的代码  -- 回调函数，当浏览器收到服务器返回的数据后才执行
					var result = eval('(' + result + ')');
					if (!result.success) {
						//新增失败，使用消息框提示错误信息
						$.messager.show({
							title : 'Error',
							msg : result.errorMsg
						});
					} else {
						$('#store_dlg').dialog('close'); // 新增成功，关闭新增对话框
						$('#store_dg').datagrid('reload'); // 新增成功，刷新列表数据
					}
				}
			});
		}

		function destroyStore() {
			var row = $('#store_dg').datagrid('getSelected');
			if (row) {
				$.messager.confirm('Confirm', '确定删除?误删今晚加班!', function(r) {
					if (r) {
						$.post('../store/delete', {
							storeNo : row.storeNo
						}, function(result) {
							if (result.success) {
								$('#store_dg').datagrid('reload'); // reload the user data
							} else {
								$.messager.show({ // show error message
									title : 'Error',
									msg : result.errorMsg
								});
							}
						}, 'json');
					}
				});
			}
		}
	</script>

</body>
</html>



